package com.dayatang.android.Db;

import java.util.ArrayList;
import java.util.List;

import com.dayatang.android.Model.Dictionary;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DictionaryServiceDb {
	
	private DBOpenHelper dbOpenHelper;

	public DictionaryServiceDb(Context context) {
		this.dbOpenHelper = new DBOpenHelper(context);
	}
	
	public void save(Dictionary dictionary) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("name", dictionary.getName());
		values.put("thisid", dictionary.getThisid());
		values.put("type", dictionary.getType());
		db.insert("dictionarys", null, values);
		db.close();
	}
	
	public List<Dictionary> getDictionaryByDictionaryType(String type) {
		
		List<Dictionary> dictionaries = new ArrayList<Dictionary>();
		
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		Cursor cursor = db.query("dictionarys", null, "type=?", new String[]{type}, null, null, null);

		while (cursor.moveToNext()) {
			Dictionary dictionary = new Dictionary();
			dictionary.setName(cursor.getString(cursor.getColumnIndex("name")));
			dictionary.setThisid(cursor.getString(cursor.getColumnIndex("thisid")));
			dictionary.setType(cursor.getString(cursor.getColumnIndex("type")));
			dictionaries.add(dictionary);
		}
		cursor.close();
		db.close();
		
		return dictionaries;
	}
	
	public void delDictionaryByType(String[] type) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		db.delete("dictionarys","type=?",type);
		db.close();
	}
	
}
